<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{

	function __construct()
        {
		parent::__construct();
		$this->load->model('Users');
		$this->load->model('Administration');
		$this->load->model('mi_facebook_model');
	}
	
	public function index()
	{
		#-----------------
					
		$this->appId = '712944462084224';
		$this->secret = '58b2806877539684ebedfbe88cb9d226';
		$fb_config = array(
		    'appId' => $this->appId,
		    'secret' => $this->secret
		);
	 
		$this->load->library('facebook', $fb_config);
	 
		$user_id = $this->facebook->getUser();
		$token = $this->facebook->getAccessToken();
	 
		$data['login_url'] = $this->facebook->getLoginUrl(array(
			'scope' => 'email,user_birthday,publish_stream,offline_access', 'redirect_uri' => 'http://prode.indev.com.ar/login/byFacebook'));
		
		#-----------------
		
		//$this->load->view('login',$data);
		
		$this->load->view('layout/header');
		$this->load->view('login/index',$data);
		$this->load->view('layout/footer');
	}
	
	public function validate()
	{
		$var = $this->Users->ValidateSession($this->input->post());
		
		if($var == 0)
		{
			echo json_encode(0);
		}
		else
		{
			$this->session->set_userdata($var);
			echo json_encode(1);
		}
	}
	
	function LogOut()
        {
            $var = array('user_data' => null,'username' => null,'email' => null, 'logged_in' => false);
            $this->session->set_userdata($var);
            $this->session->unset_userdata(null);
            $this->session->sess_destroy();
            redirect("/");    
        }
	
	function byFacebook()
	{
		$this->appId = '712944462084224';
		$this->secret = '58b2806877539684ebedfbe88cb9d226';
		$fb_config = array(
		    'appId' => $this->appId,
		    'secret' => $this->secret
		);
		$this->load->library('facebook', $fb_config);
 
		$user_id = $this->facebook->getUser();
		$token = $this->facebook->getAccessToken();
 
                $data['user_profile'] = $this->facebook->api('/me');
		$data['usuario'] = $this->mi_facebook_model->datos_usuario();
		
		$var = $this->Users->InsertFromFacebook($data);
		$this->session->set_userdata($var);
		
		redirect('/user/dashboard') ;  
	}
}

